/*
 * MatchInfo.cpp
 *
 *  Created on: 2011-1-23
 *      Author: Administrator
 */

#include "MatchInfo.h"
#include "GuiFactory.h"

CMatchInfo::CMatchInfo()
	{
	iBackImage = NULL;
	iLeftImage = NULL;
	iRightImage = NULL;
	iRace = NULL;
	}

CMatchInfo::~CMatchInfo()
	{
//	delete iBackImage;
//	iBackImage = NULL;
//	delete iLeftImage;
//	iLeftImage = NULL;
//	delete iRightImage;
//	iRightImage = NULL;
	CImageFactory::getInstance()->ReleaseImage(iBackImage);
	CImageFactory::getInstance()->ReleaseImage(iLeftImage);
	CImageFactory::getInstance()->ReleaseImage(iRightImage);
	iRace = NULL;
	}

CMatchInfo* CMatchInfo::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CMatchInfo* self = CMatchInfo::NewLC(aRect,aParent);
    CleanupStack::Pop(self);
    return self;
	}

CMatchInfo* CMatchInfo::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CMatchInfo* self = new(ELeave) CMatchInfo();
    CleanupStack::PushL(self);
    self->ConstructL(aRect,aParent);
    return self;
	}

void CMatchInfo::ConstructL(const TRect& aRect,const CCoeControl* aParent)
	{
    if(aParent == NULL)
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL(*aParent);
        }
    SetRect(aRect);
    iBackImage = CGuiFactory::GetImageL(_L("team_blue.png"));
//    iLeftImage = CGuiFactory::GetImageL(_L("1.png"));
//    iRightImage = CGuiFactory::GetImageL(_L("1.png"));
    ActivateL();
	}

void CMatchInfo::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc, aRect);
	TPoint position = Position();
	if (iRace)
		{
		TRect rect;
		TRect textRect;
		TRect ImageRect;
		TInt imgWidth = 0;
		TInt backWidth = 0;
		TInt backHeight = 0;
		rect = aRect;
		rect.SetWidth(aRect.Width()/2);
		gc.UseFont(Font());
		TInt baseline = baseline = aRect.Height() / 2 + Font()->AscentInPixels() / 2;
		//left
		if (iLeftImage)
			{
			if (iBackImage)
				CGraphicsTool::DrawImage(gc, iBackImage->Bitmap(),iBackImage->MaskBitmap(), rect,CGraphicsContext::ELeft,rect.Width()/8);
			imgWidth = iLeftImage->SizeInPixels().iWidth;
			backWidth = iBackImage->SizeInPixels().iWidth;
			backHeight = iBackImage->SizeInPixels().iHeight;
			ImageRect = rect;
			ImageRect.iTl.iX = rect.Width()/8 + 20;
			ImageRect.SetHeight(backHeight);
			ImageRect.SetWidth(backHeight);
			CGraphicsTool::DrawImage(gc, iLeftImage->Bitmap(),iLeftImage->MaskBitmap(), ImageRect,EScale);
			}
		if (iRace->Team1().Length() > 0)
			{
			gc.SetPenColor(KRgbWhite);
			textRect = rect;
			textRect.iTl.iX += (rect.Width()/8 + imgWidth + 10);
			textRect.SetWidth(backWidth - imgWidth - 10);
			gc.DrawText(iRace->Team1(), textRect, baseline, CGraphicsContext::ECenter);
			}
		if (iRace->Score1().Length() > 0)
			{
			gc.SetPenColor(KRgbYellow);
			gc.DrawText(iRace->Score1(), rect, baseline, CGraphicsContext::ERight, 10);
			}
		//mid
		gc.DrawText(_L(":"), aRect, baseline, CGraphicsContext::ECenter);
		//right
		rect = aRect;
		rect.iTl.iX += aRect.Width()/2;
		rect.SetWidth(aRect.Width()/2);
		if (iRightImage)
			{
			if (iBackImage)
				CGraphicsTool::DrawImage(gc, iBackImage->Bitmap(),iBackImage->MaskBitmap(), rect,CGraphicsContext::ERight,rect.Width()/8);
			imgWidth = iRightImage->SizeInPixels().iWidth;
			backWidth = iBackImage->SizeInPixels().iWidth;
			backHeight = iBackImage->SizeInPixels().iHeight;
			ImageRect = rect;
			ImageRect.iTl.iX += (rect.Width() - rect.Width()/8 - backWidth + 20);
			ImageRect.SetHeight(backHeight);
			ImageRect.SetWidth(backHeight);
			CGraphicsTool::DrawImage(gc, iRightImage->Bitmap(),iRightImage->MaskBitmap(), ImageRect,EScale);
			}
		if (iRace->Team2().Length() > 0)
			{
			gc.SetPenColor(KRgbWhite);
			textRect = rect;
			textRect.iTl.iX += (rect.Width() - rect.Width()/8 - backWidth + imgWidth + 10);
			textRect.SetWidth(backWidth - imgWidth - 10);
			gc.DrawText(iRace->Team2(), textRect, baseline, CGraphicsContext::ECenter);
			}
		if (iRace->Score2().Length() > 0)
			{
			gc.SetPenColor(KRgbYellow);
			gc.DrawText(iRace->Score2(), rect, baseline, CGraphicsContext::ELeft, 10);
			}
		gc.DiscardFont();
		}
	}

void CMatchInfo::SetLayout()
	{
	TRect rect = Rect();
	TInt height = Font()->HeightInPixels() + 8;
	if (iBackImage)
		{
		height = iBackImage->SizeInPixels().iHeight;
		height += 6;
		}
	if (rect.Height() < height)
		{
		rect.SetHeight(height);
		SetRect(rect);
		}
	}

void CMatchInfo::setRace(CRace* aRace)
	{
	if (aRace)
		{
		iRace = aRace;
//		delete iLeftImage;
//		iLeftImage = NULL;
		CImageFactory::getInstance()->ReleaseImage(iLeftImage);
		iLeftImage = CGuiFactory::GetTeamImageL(iRace->Team1());
//		delete iRightImage;
//		iRightImage = NULL;
		CImageFactory::getInstance()->ReleaseImage(iRightImage);
		iRightImage = CGuiFactory::GetTeamImageL(iRace->Team2());
		}
	}
